﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using StoryDesigner.Webservices;
using StoryDesigner.Controls;

namespace StoryDesigner
{
	public partial class MainPage
	{
		private List<StoryArc> _mySavedArcs;
		private StoryArc _currentStoryArc;

		public MainPage()
		{
			InitializeComponent();
			App.Client.StoryDesign_GetMyArcsCompleted += ClientStoryDesignGetMyArcsCompleted;
			//App.Client.StoryDesign_GetMyArcsAsync(App.InternalSessionId);
			grdZoomContainer.SizeChanged += GrdZoomContainerSizeChanged;
			Regex r = new Regex(@"\A(?<command>\S*)((\s+(?<position>[0-9\.]*))?|((\s+url=(?<url>\S*)){1}(\s+id=(?<callId>[0-9]+)){1})?)\Z");
		}

		void GrdZoomContainerSizeChanged(object sender, SizeChangedEventArgs e)
		{
			grdZoomContainer.SetClip();
		}

		void ClientStoryDesignGetMyArcsCompleted(object sender, StoryDesign_GetMyArcsCompletedEventArgs e)
		{
			App.Client.StoryDesign_GetMyArcsCompleted -= ClientStoryDesignGetMyArcsCompleted;
			if(e.Error != null)
			{
				App.ServerError();
				return;
			}
			if(e.Result != null)
			{
				lstMyArcs.ItemsSource = e.Result;
			}
		}

		private void BtnNewArcClick(object sender, RoutedEventArgs e)
		{
			UiManager.Instance.Clear();
			_currentStoryArc = new StoryArc
			{
				Title = "My story",
				Description = "This is a plain story arc",
				CreationDate = DateTime.Now,
				CreatorId = 12345,
				Id = 0,
				//StartingTask = new StoryTask
				//{
				//    Description = "Empty task description",
				//    Title = "New Task",
				//    State = TaskState.StartingTask
				//}
			};

			StoryArcControl item = new StoryArcControl();
			item.GotFocus += ItemGotFocus;
			item.DataContext = _currentStoryArc;
			item.Width = 300;
			item.Height = 300;
			item.NewTaskButtonVisibility = Visibility.Visible;
			_workSpace.DataContext = _currentStoryArc;
			_workSpace.Refresh();
		}

		private void ItemGotFocus(object sender, EventArgs e)
		{
			UiManager.Instance.BringToFront((StoryArcControl)sender);
		}
	}
}

namespace StoryDesigner.Webservices
{
	public partial class StoryArc
	{
		public override string ToString()
		{
			return Title;
		}
	}
}
